package com.ideaction.game.utils;

import java.util.UUID;

import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ideaction.game.globalconst.KeyPrefix;
@Slf4j
public class TokenUtil {

	public static boolean auth(RedissonClient redissonClient, String token, String clientIp, String uid) {
		String key = KeyPrefix.AdminRedisPrefix.ADMIN_USER_ID + uid;
		if (!RedissonUtils.isExists(redissonClient, key)) {
			log.info("token key 不存在 key:"+key);
			return false;
		}
		JSONObject jsonObject = RedissonUtils.get(redissonClient, key, JSONObject.class);
		String access_token = jsonObject.getString("access_token");
		log.info("redis token:"+access_token+" clientToken:"+token+" UserId:"+uid);
		if (!token.equals(access_token)) {
			return false;
		}
		return true;
	}
	
	public static String upToken(RedissonClient redissonClient,String uid,String ip){
		String access_token = getUUID();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("ip", ip);
		jsonObject.put("access_token", access_token);
		RedissonUtils.set(jsonObject, redissonClient, KeyPrefix.AdminRedisPrefix.ADMIN_USER_ID + uid, 0);
		return access_token;
	}
    public static String getUUID(){
        String uuid = UUID.randomUUID().toString();
        //去掉“-”符号 
        return uuid.replaceAll("-", "");
    }
}
